package com.doubao.wechat.client;

import com.doubao.common.result.Result;
import com.doubao.wechat.dto.response.MembershipLevelDTO;
import com.doubao.wechat.dto.response.UserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 用户服务客户端的降级实现
 */
@Component
@Slf4j
public class UserServiceClientImpl implements UserServiceClient {

    @Override
    public Result<UserInfoResponse> getUserInfo(Long userId) {
        log.warn("用户服务降级: getUserInfo({})", userId);

        // 创建一个简单的用户信息作为兜底返回
        UserInfoResponse userInfo = new UserInfoResponse();
        userInfo.setId(userId); // 更改为设置id字段
        userInfo.setUsername("localUser");
        userInfo.setNickname("本地用户");
        userInfo.setWechatBound(true); // 假设已绑定微信
        userInfo.setMiniappOpenid("local_openid_miniapp");
        userInfo.setOfficialOpenid("local_openid_official");
        userInfo.setIsVip(false); // 默认非VIP
        userInfo.setStatus(1); // 正常状态
        return Result.success(userInfo);
    }

    @Override
    public Result<MembershipLevelDTO> getMembershipLevelInfo(Long levelId) {
        log.warn("用户服务降级: getMembershipLevelInfo({})", levelId);

        // 创建一个默认的会员等级信息作为兜底返回
        MembershipLevelDTO membershipLevel = new MembershipLevelDTO();
        membershipLevel.setId(levelId.intValue());

        // 根据会员ID设置不同的默认值
        if (levelId == 1) {
            membershipLevel.setName("普通用户");
            membershipLevel.setCode("FREE");
            membershipLevel.setDailyTokenLimit(100);
            membershipLevel.setChatLimit(10);
            membershipLevel.setSwitchAgentLimit(2);
            membershipLevel.setDescription("免费用户基础权益");
        } else if (levelId == 2) {
            membershipLevel.setName("普通会员");
            membershipLevel.setCode("STANDARD");
            membershipLevel.setDailyTokenLimit(1000);
            membershipLevel.setChatLimit(100);
            membershipLevel.setSwitchAgentLimit(20);
            membershipLevel.setPriceWeekly(new BigDecimal("9.90"));
            membershipLevel.setPriceMonthly(new BigDecimal("19.90"));
            membershipLevel.setPriceQuarterly(new BigDecimal("69.90"));
            membershipLevel.setPriceYearly(new BigDecimal("239.90"));
            membershipLevel.setDescription("标准会员权益");
        } else if (levelId == 3) {
            membershipLevel.setName("高级会员");
            membershipLevel.setCode("PREMIUM");
            membershipLevel.setDailyTokenLimit(9999);
            membershipLevel.setChatLimit(999);
            membershipLevel.setSwitchAgentLimit(199);
            membershipLevel.setPriceWeekly(new BigDecimal("19.90"));
            membershipLevel.setPriceMonthly(new BigDecimal("39.90"));
            membershipLevel.setPriceQuarterly(new BigDecimal("129.90"));
            membershipLevel.setPriceYearly(new BigDecimal("339.90"));
            membershipLevel.setDescription("高级会员权益");
        } else if (levelId == 4) {
            membershipLevel.setName("临时卡");
            membershipLevel.setCode("TEMP");
            membershipLevel.setDailyTokenLimit(500);
            membershipLevel.setChatLimit(50);
            membershipLevel.setSwitchAgentLimit(10);
            membershipLevel.setPriceWeekly(new BigDecimal("2.00"));
            membershipLevel.setDescription("2元临时卡，24小时有效");
        } else {
            // 默认会员信息
            membershipLevel.setName("未知会员");
            membershipLevel.setCode("UNKNOWN");
            membershipLevel.setDailyTokenLimit(500);
            membershipLevel.setChatLimit(50);
            membershipLevel.setSwitchAgentLimit(10);
            membershipLevel.setPriceMonthly(new BigDecimal("19.90"));
            membershipLevel.setDescription("未知会员类型");
        }

        membershipLevel.setStatus(1);
        membershipLevel.setCreatedAt(LocalDateTime.now());
        membershipLevel.setUpdatedAt(LocalDateTime.now());

        log.info("返回兜底会员等级信息: {}", membershipLevel);
        return Result.success(membershipLevel);
    }

    @Override
    public Result<Void> activateMembership(Long userId, Integer levelId, String subscriptionType,
                                           Integer days, String source, String orderId) {
        log.warn("用户服务降级: activateMembership({}, {}, {}, {}, {}, {})",
                userId, levelId, subscriptionType, days, source, orderId);

        // 降级时记录失败信息，等待后续处理
        log.error("会员激活失败，需要手动处理: 用户ID={}, 会员ID={}, 订单号={}",
                userId, levelId, orderId);

        // 返回失败结果
        return Result.failed("会员服务暂时不可用，订单已记录，稍后将自动处理");
    }
}